package portal.conf.entity;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import portal.common.MyStringUtils;
@Component
@Scope("singleton")
public class HostGroupEntity extends BaseConf{
	String hostGroupName;
	String[] hosts;
	int flag=1;//1:header 0:data
	public HostGroupEntity(){}
	public HostGroupEntity(String val){
		setValue(val);
	}
	public HostGroupEntity(String hostGroupName,String[]hosts){
		this.hostGroupName=hostGroupName;
		this.hosts=hosts;
	}
	public String getHostGroupName(){
		if(hostGroupName==null)return "";
		return hostGroupName;
	}
	public HostGroupEntity setValue(String val){
		//val=MyStringUtils.trimAll(val);
		val =MyStringUtils.strictSpace(val);
		hostGroupName=val.substring(0,val.indexOf(" "));
		if(val.contains("(")){
			flag=0;
			hosts=val.substring(val.indexOf("(")+1,val.indexOf(")")).split(" ");
		}else{
			flag=1;
			hosts=val.substring(val.indexOf(" ")).trim().split(" ");
		}
		return this;
	}
	@Override
	public String toString(){
		String val="";
		val+=hostGroupName+"  \t\t";
		if(flag==0){
			val+="(";
		}
		for(String str:hosts){
			val +=str+" ";
		}
		if(flag==0){
			val+=")";
		}
		return val;
	}
	@Override
	public String getKey() {
		// TODO Auto-generated method stub
		return getHostGroupName();
	}
	public boolean addHost(String hostName){
		if(MyStringUtils.isEmpty(hostName))return false;
		return addHost(hostName.split(" "));
		/*String [] hostsNew= new String[hosts.length+1];
		System.arraycopy(hosts, 0, hostsNew, 0, hosts.length);
		hosts=hostsNew;*/
		//return true;
	}
	public boolean addHost(String[] hostInfos){
		if(null==hosts || hosts.length==0)return false;
		String [] hostsNew= new String[hosts.length+hostInfos.length];
		System.arraycopy(hosts, 0, hostsNew, 0, hosts.length);
		System.arraycopy(hostInfos, 0, hostsNew, hosts.length, hostInfos.length);
		hosts=hostsNew;
		return true;
	}
	public String[] getHosts() {
		return hosts;
	}
	public void setHostGroupName(String hostGroupName) {
		this.hostGroupName = hostGroupName;
	}
	public void setHosts(String[] hosts) {
		flag=0;
		this.hosts = hosts;
	}	
	public void setHosts(String host){
		flag=0;
		String[]h=new String[1];
		h[0]=host;
		this.hosts= h;
	}
}
